import { ref } from 'vue';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { list as deviceTypeList } from '../deviceType/deviceType.api';
import { getDeviceModelByType } from '../deviceModel/deviceModel.api';
import { getSignalByModel } from '../signal/signal.api';
import { listByModel } from '../device/device.api';
import { h } from 'vue';
import { Icon } from '/@/components/Icon';
import { render } from '/@/utils/common/renderUtils';
import { isNullOrUnDef, isEmpty } from '/@/utils/is';

export const paramDeviceTypeId = ref('');

export const columns: BasicColumn[] = [
  {
    title: '设备名称',
    dataIndex: 'deviceName',
    width: 200,
    align: 'left',
  },
  {
    title: '设备编号',
    dataIndex: 'deviceSn',
    width: 150,
    align: 'left',
  },
  {
    title: '点位地址',
    dataIndex: 'address',
    width: 120,
  },
  {
    title: '失败原因',
    dataIndex: 'reason',
    width: 280,
  },
  {
    title: '采集状态',
    dataIndex: 'status',
    width: 80,
    fixed: 'right',
    customRender: ({ text }) => {
      const color = text == '0' ? 'green' : text == '1' ? 'red' : 'gray';
      const label = text == '0' ? '成功' : text == '1' ? '失败' : '未知';
      return render.renderTag(label, color);
    },
  },
  {
    title: '采集值',
    dataIndex: 'value',
    width: 160,
    fixed: 'right'
  },
  {
    title: '采集时间',
    dataIndex: 'collectTime',
    width: 160,
    fixed: 'right'
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'modelId',
    label: '设备型号',
    component: 'ApiSelect',
    colProps: { span: 6 },
    componentProps: ({ formModel }) => {
      return {
        api: getDeviceModelByType,
        params: {typeId: paramDeviceTypeId.value},
        labelField: 'model',
        valueField: 'id',
        getPopupContainer: () => document.body,
      };
    },
  },
  {
    label: '采集设备',
    field: 'uid',
    component: 'ApiSelect',
    colProps: { span: 6 },
    componentProps: ({ formModel }) => {
      return {
        api: listByModel,
        params: {modelId: formModel.modelId},
        labelField: 'name',
        valueField: 'uid',
        getPopupContainer: () => document.body,
        onChange: (v,item) => {
          // formModel.childSn = '';
          // formModel.childDeviceId = '';
        }
      };
    },
  },
  {
    label: '点位组',
    field: 'signalId',
    component: 'ApiSelect',
    colProps: { span: 6 },
    componentProps: ({ formModel }) => {
      return {
        api: getSignalByModel,
        params: {modelId: formModel.modelId},
        labelField: 'name',
        valueField: 'id',
        getPopupContainer: () => document.body,
      };
    },
  },  
];